package com.itheima02.day11;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author 刘全
 * @date 2021/7/28
 */
public class Test1 {
    public static void main(String[] args) {
        //键盘录入一个字符串"hello world java heihei haha hehe"统计每个字符出现的次数
        String s = "helloworldjavaheiheihahahehe";
        char[] chars = s.toCharArray();
        HashMap<Character, Integer> hm = new HashMap<>();
        Set<Character> keySet = hm.keySet();
        for (int i = 0; i < chars.length; i++) {
            if (keySet.contains(chars[i])) {
                Integer value = hm.get(chars[i]);
                hm.put(chars[i], value+1);
            } else {
                hm.put(chars[i],1);
            }
        }
        Set<Map.Entry<Character, Integer>> entries = hm.entrySet();
        for (Map.Entry<Character, Integer> entry : entries) {
            Character key = entry.getKey();
            Integer value = entry.getValue();
            System.out.println("字符:" + key + "---" + "次数:" + value);
        }
    }
}
